package com.example.demo3.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo3.entity.ResourceDevice;
import com.example.demo3.mapper.ResourceDeviceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ResourceDeviceService extends ServiceImpl<ResourceDeviceMapper, ResourceDevice> {

    @Resource
    private ResourceDeviceMapper resourceDeviceMapper;

    public List<ResourceDevice> getList(){
        QueryWrapper<ResourceDevice> queryWrapper=new QueryWrapper<ResourceDevice>()
                .select("ID","RESOURCE_DEVICE_NO","RESOURCE_DEVICE_NAME","STATUS",
                        "CREATE_USER","CREATE_TIME","UPDATE_USER","UPDATE_TIME")
                //.eq("STATUS",1)
                .orderByDesc("CREATE_TIME");
        List<ResourceDevice> list = resourceDeviceMapper.selectList(queryWrapper);
        return list;
    }

    /***
     * 根据ID查询设备
     * @param id
     * @return
     */
    public ResourceDevice get(String id){
        QueryWrapper<ResourceDevice> queryWrapper = new QueryWrapper<ResourceDevice>()
                .eq("ID",id);
               // .eq("STATUS",0);
        ResourceDevice resourceDevice = resourceDeviceMapper.selectOne(queryWrapper);
        return  resourceDevice;
    }

    public void insert(ResourceDevice resourceDevice){
        resourceDeviceMapper.insert(resourceDevice);
    }

    public void update(ResourceDevice resourceDevice){
        resourceDeviceMapper.updateById(resourceDevice);
    }

    public Page<ResourceDevice> getSelectPage(String resourceDeviceNo, String resourceDeviceName, int pageindex, int pagesize){
        Page<ResourceDevice> page=new Page<ResourceDevice>().setCurrent(pageindex).setSize(pagesize);
        QueryWrapper<ResourceDevice> queryWrapper=new QueryWrapper<ResourceDevice>()
            .like(StringUtils.isNotBlank(resourceDeviceNo),"RESOURCE_DEVICE_NO",resourceDeviceNo)
            .like(StringUtils.isNotBlank(resourceDeviceName),"RESOURCE_DEVICE_NAME",resourceDeviceName)
           /* if(StringUtils.isNotBlank(resourceDeviceNo)){
                queryWrapper .eq("RESOURCE_DEVICE_NO",resourceDeviceNo);
            }
            if(StringUtils.isNotBlank(resourceDeviceName)){
                queryWrapper.eq("RESOURCE_DEVICE_NAME",resourceDeviceName);

            }*/
            .orderByDesc("RESOURCE_DEVICE_NO");
        resourceDeviceMapper.selectPage(page,queryWrapper);
        return page;
    }
    public int countPage(Map<Object,Object> map){

        return resourceDeviceMapper.countPage(map);
    }

    public void deleteById(String id){
        int result=resourceDeviceMapper.deleteById(id);
    }

    public void add(ResourceDevice resourceDevice){
        resourceDevice.setId(resourceDeviceMapper.getUUID());
        resourceDeviceMapper.insert(resourceDevice);
    }
    public void delete(String id){
        QueryWrapper<ResourceDevice> queryWrapper = new QueryWrapper<ResourceDevice>()
                .eq("ID",id);
        resourceDeviceMapper.delete(queryWrapper);
    }

    public void delBatch(String ids){
        String[] arr = StringUtils.split(ids, ",");
        List<String> idsList = Arrays.asList(arr);
        resourceDeviceMapper.deleteBatchIds(idsList);

    }
}
